//
//  ViewController.m
//  汤姆猫
//
//  Created by Jorn.Wu on 15/5/15.
//  Copyright (c) 2015年 Jorn.Wu. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *tomImageView;
- (IBAction)touchHeadOnclick:(id)sender;
- (IBAction)eatOnclick:(id)sender;
- (IBAction)fartOnclick:(id)sender;
- (IBAction)pieOnclick:(id)sender;
- (IBAction)scratchOnclick:(id)sender;
- (IBAction)drinkOnclik:(id)sender;
- (IBAction)cymbalOnclik:(id)sender;
- (IBAction)footLeftOnclick:(id)sender;
- (IBAction)footRightOnclick:(id)sender;
- (IBAction)stomachOnclick:(id)sender;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)touchHeadOnclick:(id)sender {
    
//    //    零、 如果动画正在执行，我们就直接不执行以下代码
//    if (self.tomImageView.isAnimating) {
//        return;
//    }
//    
//    //    一、创建图片数组
//    NSMutableArray *tomImages = [NSMutableArray array];
//    
//    for (int i = 0; i < 81; i ++) {
//        NSString *imageName = [NSString stringWithFormat:@"%@_%02d",@"knockout",i];
//        //        创建图片
//        //        UIImage *image = [UIImage imageNamed:imageName];//会有内存溢出，所以采用下面这种
//        
//        NSString *path = [[NSBundle mainBundle]pathForResource:imageName ofType:@"jpg"];
//        UIImage *image = [UIImage imageWithContentsOfFile:path];
//        [tomImages addObject:image];
//    }
//    
//    //    二、设置动画过程
//    //    1、执行添加动画执行图片数组；
//    [self.tomImageView setAnimationImages:tomImages];//以图片创建动画效果
//    //    2、设置动画执行时间
//    [self.tomImageView setAnimationDuration:self.tomImageView.animationImages.count * 0.1];//每张图片的执行时间为0.1秒；
//    //    3、设置动画执行次数
//    [self.tomImageView setAnimationRepeatCount:1];
//    //    4、开始动画
//    [self.tomImageView startAnimating];
//    //    5、清空图片
//    //    [self performSelector:@selector(clearImages) withObject:nil afterDelay:self.tomImageView.animationDuration];//这种方法代码较多；
//    [self.tomImageView performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:self.tomImageView.animationDuration];//设置图片数量为0；
//    为了不重复写相同的代码，自己创建以歌私有方法
    [self tomAnimateWithFileName:@"knockout" imageCount:81];
}

- (IBAction)eatOnclick:(id)sender {
    [self tomAnimateWithFileName:@"eat" imageCount:40];
}

- (IBAction)fartOnclick:(id)sender {
    [self tomAnimateWithFileName:@"fart" imageCount:28];
}

- (IBAction)pieOnclick:(id)sender {
    [self tomAnimateWithFileName:@"pie" imageCount:24];
}

- (IBAction)scratchOnclick:(id)sender {
    [self tomAnimateWithFileName:@"scratch" imageCount:56];
}

- (IBAction)drinkOnclik:(id)sender {
    [self tomAnimateWithFileName:@"drink" imageCount:81];
}

- (IBAction)cymbalOnclik:(id)sender {
    [self tomAnimateWithFileName:@"cymbal" imageCount:13];
}

- (IBAction)footLeftOnclick:(id)sender {
    [self tomAnimateWithFileName:@"footLeft" imageCount:30];
}

- (IBAction)footRightOnclick:(id)sender {
    [self tomAnimateWithFileName:@"footRight" imageCount:30];
}

- (IBAction)stomachOnclick:(id)sender {
    [self tomAnimateWithFileName:@"stomach" imageCount:34];
}

#pragma mark - 动画执行方法
- (void)tomAnimateWithFileName:(NSString *)fileName imageCount:(NSInteger)imageCount{
    //    零、 如果动画正在执行，我们就直接不执行以下代码
    if (self.tomImageView.isAnimating) {
        return;
    }
    
    //    一、创建图片数组
    NSMutableArray *tomImages = [NSMutableArray array];
    
    for (int i = 0; i < imageCount; i ++) {
        NSString *imageName = [NSString stringWithFormat:@"%@_%02d",fileName,i];
        //        创建图片
        //        UIImage *image = [UIImage imageNamed:imageName];//会有内存溢出，所以采用下面这种
        
        NSString *path = [[NSBundle mainBundle]pathForResource:imageName ofType:@"jpg"];
        UIImage *image = [UIImage imageWithContentsOfFile:path];
        [tomImages addObject:image];
    }
    
    //    二、设置动画过程
    //    1、执行添加动画执行图片数组；
    [self.tomImageView setAnimationImages:tomImages];//以图片创建动画效果
    //    2、设置动画执行时间
    [self.tomImageView setAnimationDuration:self.tomImageView.animationImages.count * 0.1];//每张图片的执行时间为0.1秒；
    //    3、设置动画执行次数
    [self.tomImageView setAnimationRepeatCount:1];
    //    4、开始动画
    [self.tomImageView startAnimating];
    //    5、清空图片
    //    [self performSelector:@selector(clearImages) withObject:nil afterDelay:self.tomImageView.animationDuration];//这种方法代码较多；
    [self.tomImageView performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:self.tomImageView.animationDuration];//设置图片数量为0；
}

//- (void)clearImages{
//    self.tomImageView.animationImages = nil;//清空
//}

@end
